package com.zwxbc.funplay.servlet;

import com.zwxbc.funplay.entity.Admin;
import com.zwxbc.funplay.entity.Developer;
import com.zwxbc.funplay.entity.Player;
import com.zwxbc.funplay.service.AdminService;
import com.zwxbc.funplay.service.DeveloperService;
import com.zwxbc.funplay.service.PlayerService;
import com.zwxbc.funplay.service.impl.AdminServiceImpl;
import com.zwxbc.funplay.service.impl.DeveloperServiceImpl;
import com.zwxbc.funplay.service.impl.PlayerServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String identity = request.getParameter("identity");
			String username = request.getParameter("username");
			String password = request.getParameter("password");

			switch (identity) {
				case "admin":
					AdminService adminService = new AdminServiceImpl();
					Admin admin = new Admin();
					admin.setAdminUsername(username);
					admin.setAdminPassword(password);
					Admin authenticatedAdmin = adminService.adminLogin(admin);
					if (authenticatedAdmin != null) {
						// 登录成功
						request.getSession().setAttribute("user", authenticatedAdmin);

						response.sendRedirect(request.getContextPath() + "/view/manage/adminManage.jsp");

					} else {
						// 登录失败
						String errorMessage = "账号或密码错误";
						request.setAttribute("errorMessage", errorMessage);

						// 转发回登录页面
						request.getRequestDispatcher("/login.jsp").forward(request, response);
					}
					break;
				case "player":
					Player player = new Player();
					player.setPlayerPassword(password);
					player.setPlayerUsername(username);
					PlayerService playerService = new PlayerServiceImpl();
					Player authenticatedPlayer = playerService.playerLogin(player);
					if (authenticatedPlayer != null) {
						// 登录成功
						request.getSession().setAttribute("user", authenticatedPlayer);

						response.sendRedirect(request.getContextPath() + "/view/player/home.jsp");

					} else {
						// 登录失败
						String errorMessage = "账号或密码错误";
						request.setAttribute("errorMessage", errorMessage);

						// 转发回登录页面
						request.getRequestDispatcher("/login.jsp").forward(request, response);
					}
					break;
				case "developer":
					DeveloperService developerService = new DeveloperServiceImpl();
					Developer developer = new Developer();
					developer.setDeveloperUsername(username);
					developer.setDeveloperPassword(password);
					Developer authenticatedLogin = developerService.developerLogin(developer);
					handleLoginResponse(authenticatedLogin, request, response);
					break;
				default:
					response.setContentType("application/json");
					response.setCharacterEncoding("UTF-8");
					response.getWriter().write("{\"error\": \"未选择身份\"}");
					break;
			}
		} catch (Exception e) {
			e.printStackTrace();
			response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "请求错误");
		}
	}

	private void handleLoginResponse(Object user, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		if (user != null) {
			// 登录成功
			request.getSession().setAttribute("user", user);

			response.sendRedirect(request.getContextPath() + "/view/player/home.jsp");

		} else {
			// 登录失败
			String errorMessage = "账号或密码错误";
			request.setAttribute("errorMessage", errorMessage);

			// 转发回登录页面
			request.getRequestDispatcher("/login.jsp").forward(request, response);
		}
	}
}


